package com.tanhua.dubbo.api;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tanhua.dubbo.mappers.FreezeMapper;
import com.tanhua.model.db.Freeze;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

@DubboService
public class FreezeApilmpl implements FreezeApi {
    @Autowired
    private FreezeMapper freezeMapper;
    @Override
    public void freezeUser(Freeze freeze) {
        //冻结用户
        if (freeze.getFreezingTime().equals("1")) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DAY_OF_MONTH, 3);
        Date time = c.getTime();
        String untieTime = sdf.format(time);
        freeze.setUntieTime(untieTime);

        }else if(freeze.getFreezingTime().equals("2")){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DAY_OF_MONTH, 7);
        Date time = c.getTime();
        String untieTime = sdf.format(time);
        freeze.setUntieTime(untieTime);
        }else {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar c = Calendar.getInstance();
        c.add(Calendar.YEAR, 1000);
        Date time = c.getTime();
        String untieTime = sdf.format(time);
        freeze.setUntieTime(untieTime);
        }
        //获取冻结范围    冻结范围，1为冻结登录，2为冻结发言，3为冻结发布动态
       if(freeze.getFreezingRange().equals("1")){
        freeze.setFreezingRange("冻结登录");
        }else if(freeze.getFreezingRange().equals("2")){
        freeze.setFreezingRange("冻结发言");
        }else{
        freeze.setFreezingRange("冻结发布动态");
        }
        freezeMapper.insert(freeze);
    }

    @Override
    public Freeze findById(Long userId) {
        LambdaQueryWrapper<Freeze> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Freeze::getUserId,userId);
        return freezeMapper.selectOne(wrapper);
    }

    @Override
    public void deleteUser(Long id) {
        LambdaQueryWrapper<Freeze> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Freeze::getUserId,id);
         freezeMapper.delete(wrapper);
    }


}
